<?php
class ModelModuleOpenIdAuth extends Model 
{
	public function getUserInfo( $provider, $identifier, $newuser_email )
	{	
		$user_id = $this->getUserId( $user_id );
		if(!$user_id>0)
			return false;
			
		$user_info = $this->getUser($user_id);
		$user_info = !empty($user_info) ? $user_info : false;
		
		return $user_info;
	}
	
	public function getUserId( $user_id )
	{
		if($user_id>0) 
		{
			$query 		= "SELECT customer_id FROM `" . DB_PREFIX . "customer` WHERE customer_id = '" . $user_id . "' LIMIT 1";
			$res 		= $this->db->query( $query );
			$user_id 	= !empty($res->row['customer_id']) ? (int)$res->row['customer_id'] : 0;
			return $user_id;
		}
		return false;
	}
	
	public function addCustomer( $user_data ) 
	{
		$query = "INSERT INTO `" . DB_PREFIX . "customer` SET 
					firstname  = '" . $this->db->escape($user_data['firstname']) . "',
					lastname = '" . $this->db->escape($user_data['lastname']) . "', 
					email = '" . $this->db->escape($user_data['email']) . "', 
					password = '" . $this->db->escape(md5($user_data['password'])) . "',  
					customer_group_id = '" . (int)$user_data['customer_group_id'] . "', 
					status = '" . (int)$user_data['status'] . "',
					approved  = '" . (int)$user_data['approved'] . "',  
					date_added = NOW()
				";
		
		$this->db->query($query);
		return (int)$this->db->getLastId();
	}
	
	public function getCustomerGroupId()
	{
		$customer_group_id = $this->config->get('config_customer_group_id');
		
		if($customer_group_id)
			return $customer_group_id;
		
		$query 				= "SELECT customer_group_id  FROM `" . DB_PREFIX . "customer_group` WHERE name = 'Default' LIMIT 1";
		$result				= $this->db->query( $query );
		$customer_group_id 	= isset($result->row['customer_group_id']) ? (int)$result->row['customer_group_id'] : 0;
		
		return $customer_group_id;
	}
	
	public function getUser($user_id) 
	{
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE customer_id = '" . (int)$user_id . "' LIMIT 1");
	
		return $query->row;
	}
	public function getCustomer($customer_id) 
	{
		$query = $this->db->query("SELECT DISTINCT * FROM ".DB_PREFIX."customer WHERE customer_id='".(int)$customer_id."' LIMIT 1");
	
		return $query->row;
	}
	
	public function getCustomerByEmail($customer_email) 
	{
		$query = $this->db->query("SELECT DISTINCT * FROM ".DB_PREFIX."customer WHERE email='".$customer_email."' LIMIT 1");
	
		return $query->row;
	}
	
	public function checkUsernameExist( $username )
	{
		$query 		= "SELECT customer_id  FROM `" . DB_PREFIX . "customer` WHERE username  = '" . $username . "' LIMIT 1";
		$result		= $this->db->query( $query );
		$return 	= !empty($result->row['customer_id']) && (int)$result->row['customer_id']>0 ? true : false;
		return $return;		
	}

}
?>